/*
 * $Id: unxapi.h 68 2009-02-25 03:15:52Z hyunghwan.chung $
 *
   Copyright 2005-2009 Chung, Hyung-Hwan.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an  BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

#ifndef _XP_BAS_UNXAPI_H_
#define _XP_BAS_UNXAPI_H_

#include <xp/types.h>
#include <xp/macros.h>

#ifdef __unix

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>

/* xp_pid_t */
#if XP_SIZEOF_PID_T == XP_SIZEOF_CHAR
	typedef char xp_pid_t;
#elif XP_SIZEOF_PID_T == XP_SIZEOF_SHORT
	typedef short xp_pid_t;
#elif XP_SIZEOF_PID_T == XP_SIZEOF_INT
	typedef int xp_pid_t;
#elif XP_SIZEOF_PID_T == XP_SIZEOF_LONG
	typedef long xp_pid_t;
#elif XP_SIZEOF_PID_T == XP_SIZEOF_LONG_LONG
	typedef long long xp_pid_t;
#else
	typedef int xp_pid_t; /* most of unices use int for pid */
#endif

#define XP_HND_INVALID (-1)
typedef int xp_hnd_t;

typedef uid_t  xp_uid_t;
typedef gid_t  xp_gid_t;
typedef mode_t xp_mode_t;
typedef xp_int64_t  xp_off_t;

#if !defined(_LP64) && defined(O_LARGEFILE)
typedef struct stat64 xp_stat_t;
#else
typedef struct stat xp_stat_t;
#endif

/* maximum path length */
#if /*defined(_SCO_DS) ||*/ !defined(PATH_MAX)
	/*pathconf(_PC_PATH_MAX) returns 1024 on sco osr5. large enough */
	#define XP_PATH_MAX 4096
#else
	#define XP_PATH_MAX (PATH_MAX - 1)
#endif

/* "option" values for xp_waitpid. can be ORed with each other. */
#define XP_WNOHANG    WNOHANG
#define XP_WUNTRACED  WUNTRACED

#ifdef WIFEXITED
	#define XP_WIFEXITED(x) (WIFEXITED(x))
#endif
#ifdef WEXITSTATUS
	#define XP_WEXITCODE(x) (WEXITSTATUS(x))
#endif
#ifdef WIFSIGNALED
	#define XP_WIFSIGNALED(x) (WIFSIGNALED(x))
#endif
#ifdef WTERMSIG
	#define XP_WTERMSIG(x) (WTERMSIG(x))
#endif
#ifdef WIFSTOPPED
	#define XP_WIFSTOPPED(x) (WIFSTOPPED(x))
#endif
#ifdef WSTOPSIG
	#define XP_WSTOPSIG(x) (WSTOPSIG(x))
#endif
#ifdef WCOREDUMP
	#define XP_WCOREDUMP(x) (WCOREDUMP(x))
#endif

#ifdef __cplusplus
extern "C" {
#endif

xp_uid_t xp_getuid (void);
xp_uid_t xp_geteuid (void);
xp_gid_t xp_getgid (void);
xp_gid_t xp_getegid (void);

xp_pid_t xp_setsid (void);
xp_pid_t xp_getpid (void);
xp_pid_t xp_getppid (void);

int xp_setpgid (xp_pid_t pid, xp_pid_t pgid);
xp_pid_t xp_getpgid (xp_pid_t pid);
int xp_setpgrp (void);
xp_pid_t xp_getpgrp (void);

xp_pid_t xp_fork (void);
xp_pid_t xp_waitpid (xp_pid_t pid, int *status, int options);

int xp_pipe (int pfd[2]);
xp_mode_t xp_umask (xp_mode_t mode);

#ifdef __cplusplus
}
#endif

#endif /* __unix */

#endif /* _XP_BAS_UNXAPI_H_ */
